package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.IRecruitBizService;
import com.kk.xhr.admin.controller.request.RecruitAddRequest;
import com.kk.xhr.admin.controller.request.RecruitModifyRequest;
import com.kk.xhr.admin.controller.request.RecruitQueryRequest;
import com.kk.xhr.admin.controller.vo.RecruitVO;
import com.kk.xhr.common.ErrorCodeEnum;
import com.kk.xhr.common.exception.BusinessException;
import com.kk.xhr.common.pojo.RecruitStatusEnum;
import com.kk.xhr.model.dto.PageDto;
import com.kk.xhr.model.dto.RecruitDTO;
import com.kk.xhr.model.dto.RecruitQueryRequestDTO;
import com.kk.xhr.model.service.INotificationService;
import com.kk.xhr.model.service.IRecruitService;
import com.kk.xhr.web.vo.PageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 招聘biz服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/4/6
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class RecruitBizServiceImpl implements IRecruitBizService {
    private final IRecruitService recruitService;
    private final INotificationService notificationService;

    @Override
    public PageVO<RecruitVO> queryPage(RecruitQueryRequest request) {
        PageDto<RecruitDTO> dtos = recruitService.queryPage(RecruitQueryRequestDTO.builder()
            .title(request.getTitle())
            .positionId(request.getPositionId())
            .from(request.getFrom())
            .status(request.getStatus())
            .offset(request.getOffset())
            .limit(request.getLimit())
            .build());
        if (0 == dtos.getTotal()) {
            return PageVO.empty();
        }
        return PageVO.fromDTO(dtos, this::generateVO);
    }

    @Override
    public PageVO<RecruitVO> queryMine(Long userId, RecruitQueryRequest request) {
        PageDto<RecruitDTO> dtos = recruitService.queryPage(userId, RecruitQueryRequestDTO.builder()
            .title(request.getTitle())
            .positionId(request.getPositionId())
            .from(request.getFrom())
            .status(request.getStatus())
            .offset(request.getOffset())
            .limit(request.getLimit())
            .build());
        if (0 == dtos.getTotal()) {
            return PageVO.empty();
        }
        return PageVO.fromDTO(dtos, this::generateVO);
    }

    @Override
    public void add(RecruitAddRequest request) {
        recruitService.add(request.getUserIds(), RecruitDTO.builder()
            .title(request.getTitle())
            .remark(request.getRemark())
            .endDate(request.getEndDate())
            .form(request.getForm())
            .total(request.getTotal())
            .positionId(request.getPositionId())
            .status(RecruitStatusEnum.APPLYING)
            .build());
    }

    @Override
    public void modify(Long id, RecruitModifyRequest request) {
        recruitService.modify(RecruitDTO.builder()
            .id(id)
            .title(request.getTitle())
            .remark(request.getRemark())
            .endDate(request.getEndDate())
            .form(request.getForm())
            .total(request.getTotal())
            .build());
    }

    @Override
    public void modifyStatus(Long id, RecruitStatusEnum status) {
        RecruitDTO recruit = recruitService.query(id)
            .orElseThrow(() -> new BusinessException(ErrorCodeEnum.RECRUIT_NOT_EXIST));
        if (recruit.getStatus() == status) {
            return;
        }
        if (RecruitStatusEnum.FAIL == status) {
            recruitService.modifyStatus(id, status);
            recruitService.deleteRef(id);
        }else {
            recruitService.modifyStatus(id, status);
        }
    }

    private RecruitVO generateVO(RecruitDTO dto) {
        return RecruitVO.builder()
            .id(dto.getId())
            .title(dto.getTitle())
            .remark(dto.getRemark())
            .endDate(dto.getEndDate())
            .passNum(dto.getPassNum())
            .total(dto.getTotal())
            .positionId(dto.getPositionId())
            .form(dto.getForm())
            .status(dto.getStatus())
            .build();
    }
}
